/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package model.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.dao.services.ProjectTypeDAOServices;
import model.entities.ProjectType;

/**
 *
 * @author Hung
 */
public class ProjectTypeDAO implements ProjectTypeDAOServices {

    private static ProjectTypeDAO projectTypeDAO;
    private Connection conn;
    private PreparedStatement pstm;
    private ResultSet rs;
    private Statement sttm;

    public static ProjectTypeDAO getInstance() {
        if (projectTypeDAO == null) {
            projectTypeDAO = new ProjectTypeDAO();
        }
        return projectTypeDAO;
    }

    @Override
    public List<ProjectType> getProjectType() {

        List<ProjectType> listProjectType = new ArrayList<ProjectType>();
        try {

            conn = ConnectionFactory.getConnection();
            String sql = "SELECT * FROM tblProjectType";
            pstm = conn.prepareStatement(sql);
            rs = pstm.executeQuery();
            while (rs.next()) {
                ProjectType proty = new ProjectType();
                proty.setIdProjectType(rs.getInt("idProjectType"));
                proty.setName(rs.getString("name"));
                proty.setDescriptions(rs.getString("descriptions"));
                proty.setIsActived(rs.getBoolean("isActived"));

                listProjectType.add(proty);
            }

        } catch (Exception ex) {
            Logger.getLogger(ProjectTypeDAO.class.getName()).log(Level.SEVERE, null, ex);
            Logger.getLogger(ProjectTypeDAO.class.getName()).log(Level.SEVERE, null, ex);
        }

        return listProjectType;
    }

    @Override
    public ProjectType getProjectTypeById(int idProjectType) {
        ProjectType pro = new ProjectType();
        try {
            conn = ConnectionFactory.getConnection();
            String sql = "SELECT * FROM tblProjectType WHERE idProjectType= " + idProjectType;
            pstm = conn.prepareStatement(sql);
            rs = pstm.executeQuery();
            while (rs.next()) {
                pro.setIdProjectType(rs.getInt("idProjectType"));
                pro.setName(rs.getString("name"));
                pro.setDescriptions(rs.getString("descriptions"));
                pro.setIsActived(rs.getBoolean("isActived"));

            }
        } catch (Exception ex) {
            Logger.getLogger(ProjectTypeDAO.class.getName()).log(Level.SEVERE, null, ex);
            Logger.getLogger(ProjectTypeDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return pro;

    }

    @Override
    public boolean createProjectType(ProjectType porjectType) {
        try {
            conn = ConnectionFactory.getConnection();
            String sql = "INSERT INTO tblProjectType VALUES (?, ?, ?)";
            pstm = conn.prepareStatement(sql);
            pstm.setString(1, porjectType.getName());
            pstm.setString(2, porjectType.getDescriptions());
            pstm.setBoolean(3, porjectType.isIsActived());
            pstm.executeUpdate();
        } catch (Exception ex) {
            Logger.getLogger(ProjectTypeDAO.class.getName()).log(Level.SEVERE, null, ex);
            Logger.getLogger(ProjectTypeDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return true;
    }

    @Override
    public boolean updateProjectType(ProjectType projectType, int idProjectType) {
        try {
            conn = ConnectionFactory.getConnection();
            String sql = "UPDATE tblProjectType SET name = ? , descriptions = ? , isActived = ? WHERE idProjectType=" + idProjectType;
            pstm = conn.prepareStatement(sql);

            pstm.setString(1, projectType.getName());
            pstm.setString(2, projectType.getDescriptions());
            pstm.setBoolean(3, projectType.isIsActived());
            pstm.executeUpdate();
        } catch (Exception ex) {
            Logger.getLogger(ProjectTypeDAO.class.getName()).log(Level.SEVERE, null, ex);
            Logger.getLogger(ProjectTypeDAO.class.getName()).log(Level.SEVERE, null, ex);
        }

        return true;
    }

    @Override
    public boolean reProjectType(int idProjectType, boolean isActived) {
        try {
            conn = ConnectionFactory.getConnection();
            String sql = "UPDATE tblProjectType SET isActived = ? WHERE idProjectType = " + idProjectType;
            pstm = conn.prepareStatement(sql);
            pstm.setBoolean(1, isActived);
            pstm.executeUpdate();
        } catch (Exception ex) {
            Logger.getLogger(ProjectTypeDAO.class.getName()).log(Level.SEVERE, null, ex);
            Logger.getLogger(ProjectTypeDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return true;
    }

    @Override
    public boolean deleteProjectType(int idProjectType) {
        try {
            conn = ConnectionFactory.getConnection();
            String sql = "DELETE tblProjectType WHERE [idProjectType] =" + idProjectType;
            sttm = conn.createStatement();
            sttm.executeUpdate(sql);
        } catch (Exception ex) {
            Logger.getLogger(ProjectTypeDAO.class.getName()).log(Level.SEVERE, null, ex);
            Logger.getLogger(ProjectTypeDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return true;
    }

}
